<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>固定车主管理</el-breadcrumb-item>
      <el-breadcrumb-item>出入场设置</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 添加出场信息 -->
    <el-card class="box-card">
      <el-row :gutter="20"><h3>添加入场信息</h3></el-row>
      <el-row :gutter="1">
        <el-col :span="7">
          <label>IC卡号：</label>
          <el-select v-model="cardId" placeholder="请选择" style="width: 245px">
            <el-option
              v-for="card in cardInfo"
              :key="card.cardId"
              :label="card.cardId + '(' + card.userName + ')'"
              :value="card.cardId"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="1">
          <el-button @click="addEntry()">确定</el-button></el-col
        >
      </el-row>
    </el-card>

    <!-- 出场管理 -->
    <el-card class="box-card">
      <!-- 列表区域 -->
      <el-table :data="fixedList" border stripe>
        <el-table-column type="index" label="编号"></el-table-column>
        <el-table-column prop="fixedId" label="记录编号"> </el-table-column>
        <el-table-column prop="cardId" label="IC卡号"> </el-table-column>
        <el-table-column prop="userName" label="车主名称"> </el-table-column>
        <el-table-column prop="cardNum" label="车牌号码"> </el-table-column>
        <el-table-column
          prop="entryTime"
          :formatter="dateFormat"
          label="入场日期"
        >
        </el-table-column>
        <el-table-column label="是否出场">
          <template slot-scope="scope">
            {{ scope.row.status == 0 ? '未出场' : '已出场' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <!-- 出场 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="出场"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                @click="fixedOutById(scope.row.fixedId)"
                :disabled="scope.row.status == 1 ? true : false"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import moment from 'moment'

export default {
  data() {
    return {
      cardId: '',
      cardInfo: [],
      fixedList: [],
      total: null,
      //   当前的页数
      pageNum: 1,
      //   当前每页显示多少条数据
      pageSize: 10,
    }
  },
  mounted() {
    this.getCardInfo()
    this.getFixedList()
  },
  methods: {
    async getCardInfo() {
      const { data: res } = await this.$http.get('card/listAll')
      this.cardInfo = res
    },
    // 添加入场信息
    addEntry() {
      this.$http.post('fix/entry/' + this.cardId).then((result) => {
        this.$message.success('入场成功')
        this.getFixedList()
      })
    },
    async getFixedList() {
      const { data: res } = await this.$http.post('fix/list', {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      })
      this.total = res.total
      this.fixedList = res.list
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.getFixedList()
    },
    handleCurrentChange(newPageNum) {
      this.pageNum = newPageNum
      this.getFixedList()
    },
    dateFormat(date) {
      return moment(date.entryTime).format('YYYY-MM-DD HH:mm:ss')
    },
    // 出场
    async fixedOutById(fixId) {
      this.$http.post('fix/out/' + fixId).then((result) => {
        this.$message.success('出场成功')
        this.getFixedList()
      })
    },
  },
}
</script>

<style lang="less" scoped>
</style>
